<?php
// articlelist.ob.php - Table of Contents of articles as class / object

class ArticleList {
	var $variant;
	var $alias;
	var $id;
	
	var $title;
	var $catparams;
	var $record;
	
	var $has_photo = true;
	var $for_author = false;
	var $for_category = false;

	function __construct($avariant='ALL', $analias='', $anid=0){
		// Does nothing except initializing object - and some other.
		$this->variant = $avariant;
		$this->alias = $analias;
		$this->id = $anid;
		if ($avariant=='CATEGORY') {
			$this->catparams = $this->category_name_descr($anid);
			$this->title = $this->catparams[0];
			$this->for_category = true;
		}
	}
	function show_content(){
		/*
			Variants:
			ALL, AUTHOR, CATEGORY, THREAD, TAG
			
		*/
		
		switch($this->variant){
			case 'ALL':
				$where = '';
				$html = '<br /><h1>Content</h1>'."\n\r";
				break;
				
			case 'AUTHOR':
				$where = " AND profile.alias = '". $this->alias ."' ";
				$this->has_photo = false;
				$this->for_author = true;
				$html = '<br /><h1 class="subh1">Articles:</h1>'."\n\r";
				break;
				
			case 'CATEGORY':
				$html = '<br /><h1><span class="subh1">Category:</span> '. $this->catparams[0] .'</h1>'."\n\r";
				if (!empty($this->catparams[1])) $html .= '<div class="profile_cont">'. $this->catparams[1] .'</div>'."\n\r";
				$where = " AND category = '". $this->id ."' ";
				break;
				
		}
		$query = "SELECT article.alias AS artalias, article.title, article.subtitle, UNIX_TIMESTAMP(article.ini_date) AS udate,
				article.category, category.name AS catname,
				profile.name, profile.alias AS authalias, profile.photo
				FROM article
				LEFT OUTER JOIN profile ON profile.id=article.author
				JOIN category ON category.id=article.category
				WHERE profile.name IS NOT NULL AND listed='1' "
				. $where .
				" ORDER BY ini_date DESC";
		$res = mysql_query($query);
		$num_rows = mysql_num_rows($res);
		// $html .= '<p>NUM ROWS = '.$num_rows. '</p>';
		if ($num_rows>0){
		// if (true){
			$html .= '<ul class="articlelist">'."\n\r";
			while ($row = mysql_fetch_array($res)){
				$html .= $this->item($row['authalias'], $row['photo'], $row['udate'], $row['name'], $row['artalias'], $row['title'],
					$row['subtitle'], $row['category'], $row['catname']);
			}
			$html .= "\n\r".'</ul>';
		} else {
			//$html = '<p>Brak NUM ROWS.</p>';
			$html = '';
		}
		return $html;
	}
	private function category_link($catid, $catname){
		$cont = '';
		$cont .= ' (<a href="?category='. $catid .'" title="category...">&rarr;&nbsp;'. $catname .'</a>)';
		return $cont;
	}
	private function item($authalias, $photo, $date, $authname, $artalias, $title, $subtitle, $catid, $catname){
		$aprofile = '<a href="?profile='.$authalias.'" title="'.$authname.' - Author\'s profile">';
		if ($this->has_photo)
			$author_graph = empty($photo)? '<img src="graph/accgrey.png" border="0" class="no_photo_small" />':
				$aprofile.'<img src="graph/author_photo/'.$photo.'" alt="[photo]" border="0" class="photo_small" width="40"'.' /></a> ';
		
		$html = '<li>'.$author_graph. date("j M Y", $date).'. ';
		if (!$this->for_author) $html .= $aprofile.$authname.'</a>.';
		if (!empty($subtitle)) $subtitle = '. '. $subtitle;
		$html .= ' <b><a href="?id='.$artalias.'">'.$title.'</a></b>'. $subtitle;
		if (!$this->for_category) $html .= $this->category_link($catid, $catname);
		$html .= '</li>'."\n\r";
//		(<a href="spis.php?cat=51" title="kategoria...">&rarr; Ekologia</a>)
		return $html;
	}
	private function category_name_descr($catid){
		// Returns `name` and `descr` from `category` table
		$query = "SELECT `name`, `descr` FROM `category` WHERE id='$catid'";
		$res = mysql_query($query);
		$row = mysql_fetch_row($res);
		return $row;
	}

}
?>
